<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="Lock" />
        </el-form-item>
        <el-form-item prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="管理员" value="0" />
            <el-option label="用户" value="1" />
            <el-option label="维修人员" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
        <div class="register-link">
          <router-link to="/auth/register">没有账号？立即注册</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { listUser } from '@/api/user.js'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const router = useRouter()
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password: '',
  role: '1' // 默认选择用户角色
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await listUser({
          userName: loginForm.username,
          password: loginForm.password,
          role: loginForm.role
        })
        
        if (res && res?.length !== 0 ) {
          const user = res[0]
          console.log('useruser',user)
          ElMessage.success('登录成功')
          console.log(res)
          // 根据角色跳转到不同页面
          switch (loginForm.role) {
            case '0':
              router.push('/admin/announcement')
              break
            case '1':
              router.push('/user')
              break
            case '2':
              router.push('/repair')
              break
          }
          // 存储用户信息到全局状态
          userStore.setUserInfo({
            id: user.id,
            role: user.role,
            username: user.username
          })
          console.log(userStore)
        } else {
          ElMessage.error('用户名或密码错误')
        }
      } catch (error) {
        ElMessage.error('登录失败: ' + error.message)
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
}

.register-link {
  text-align: center;
  margin-top: 15px;
}
</style>